const service = require('./personalhomepageService.js')
const utilService = require('../../../utils/utilService.js');
var app = getApp();
/** 上架 */
const UP_STATUS = 2
/** 下架 */
const DOWN_STATUS = -1
/** 审核中 */
const EXAMINEING_STATUS = 0
/**审核未通过 */
const EXAMINE_NO_STATUS = 1
/**已赠送 */
const SEND_STATUS = 4

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["分享宝贝", "感谢视频"],
    listTabs: ["已发布", "已下架", "审核中", "未过审"],
    activeIndex: 0,
    tabIndex: 0,
    favStorys: null,
    goodsListArray: [],
    tThanksArray: [],
    hiddenView: false,
    focusData: null,
    homeHidden: false,
    types: 1,
    shareMsg: '我发现了一个闲置物品的好平台【趣闲】，特意分享给你！',
    foid: null,
    focusid: null,
    isLoading: true,
    //分页
    hasNextPage: true,
    startPage: 0,  //开始

    up_status: UP_STATUS,
    send_status: SEND_STATUS,
    down_status: DOWN_STATUS,
    examineing_status: EXAMINEING_STATUS,
    examine_no_status: EXAMINE_NO_STATUS,
  },
  onLoad: function (options) {
    var self = this;
    this.setData({
      focusid: options
    })
    //授权
    self.authAlert = self.selectComponent("#authAlert");
    self.authAlert.bindCatch()
    app.loginPromise.then(() => {
      self.getShareMessage(8);
      this.requestSource(false);
    });
  },
  onShow: function () {

  },
  authSuccess() {
    console.log("授权 信息", app.globalData.userInfo)
    this.getShareMessage(8);
    this.requestSource(false)
  },

  setSource: function (options, status, loadMore) {
    var that = this
    let types = options.type ? options.type : this.data.types;
    if (types == 1) {
      that.setData({ homeHidden: true })
    } else {
      that.setData({ homeHidden: false })
    }
    var userid;
    var requestStatus = status
    if (options.userId){
      that.setData({ foid: options.userId })

    }else{
      that.setData({ foid: app.globalData.userId })
    }
    if (options.userId == app.globalData.userId) {
      that.setData({ hiddenView: true }),
        userid = app.globalData.userId
    } else if (options.userId == null) {
      that.setData({ hiddenView: true }),
        userid = app.globalData.userId
    } else {
      requestStatus = 2;
      that.setData({ hiddenView: false });
      userid = options.userId,
        this.focusService();
    }
    var parm = {
      userId: userid,
      startPage: that.data.startPage,
      status: requestStatus
    }
    service.getInformation(parm, function (data) {
      let goodsList = that.data.goodsListArray.concat(data.goodsListEntities);
      let tThanksData = that.data.tThanksArray.concat(data.tThanksStoryEntities);
      that.setData({
        favStorys: data,
        goodsListArray: goodsList,
        tThanksArray: tThanksData,
        isLoading: false,
      })
      that.titleL = data.nickName
      that.level = data.userGrade
      if (data.goodsListEntities.length < 20) {
        that.setData({
          hasNextPage: false, isLoading: false
        })
      } else {
        that.setData({
          hasNextPage: true, isLoading: false
        })
      }

      wx.setNavigationBarTitle({
        title: data.nickName,
      })
    },
      function (error) {
        wx.showToast({
          title: error.data.message,
          icon: 'none',
          duration: 2000,
        })
      }
    );

  },

  showInfo: function (data) {

  },

  //是否关注
  focusService: function () {
    var that = this;
    var parma = {
      userId: app.globalData.userId,
      foucesUserId: that.data.foid
    };
    service.requestFocus(parma, function (data) {
      that.setData({ focusData: data });
    });
  },
  //私信点击
  messBtn: function () {
    if (this.data.focusData == 0) {
      //未关注 显示  +关注
      this.showTost('关注后私信');
    } else {
      //关注  显示  未关注
      wx.navigateTo({
        url: '/pages/mine/message/msgDetails/msgDetails?fromUserId=' + this.data.foid + '&nickName=' + this.titleL
      })
    }

  },
  //商品列表item 点击事件
  itemDidSelect: function (e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/goods/pages/goodsDetails/goodsDetails?goods_id=" + e.detail.detail.id + '&goods_type=' + e.detail.detail.saleTypeId
    })
  },
  //粉丝点击
  fansDetail: function () {
    if (this.data.foid) {
      wx.navigateTo({
        url: './fansDetail/fansDetail?title=' + this.titleL + '&user_id=' + this.data.foid + '&type=' + 2,
      })
    } else {
      wx.navigateTo({
        url: './fansDetail/fansDetail?title=' + this.titleL + '&user_id=' + app.globalData.userId + '&type=' + 1,
      })
    }

  },
  //等级点击
  levelBtn: function () {
    var level = parseInt(this.level) > 0 ? this.level : 0
    wx.navigateTo({
      url: '../level/level?currentLevel=' + level
    })
  },
  tabClick(e) {
    this.setData({
      activeIndex: e.currentTarget.id
    });
  },
  listTabClick(e) {
    console.log(e)
    this.setData({
      tabIndex: e.currentTarget.id,
      startPage: 0,
      isLoading: true,
    });
    this.requestSource(false)
  },

  /**
   * 请求数据
   */
  requestSource: function (loadMore) {
    // "已发布","已下架", "审核中", "未过审"
    // -1 下架，0 审核中 ，1 审核未通过，2 上架
    if (loadMore != true) {
      this.setData({
        startPage: 0,
        goodsListArray: [],
        tThanksArray: [],
      })
    }
    if (this.data.tabIndex == 0) {
      this.setSource(this.data.focusid, UP_STATUS, loadMore)
    } else if (this.data.tabIndex == 1) {
      this.setSource(this.data.focusid, DOWN_STATUS, loadMore)
    } else if (this.data.tabIndex == 2) {
      this.setSource(this.data.focusid, EXAMINEING_STATUS, loadMore)
    } else if (this.data.tabIndex == 3) {
      this.setSource(this.data.focusid, EXAMINE_NO_STATUS, loadMore)
    }
  },

  //关注点击事件
  fansBtn: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.data)

    if (e.currentTarget.dataset.data == 0) {
      this.addFansService(that.data.foid, 0)
    } else {
      this.addFansService(that.data.foid, 1)
    }
  },

  //添加取消粉丝
  addFansService: function (focus, state) {
    var that = this;
    var parma = {
      userId: app.globalData.userId,
      foucesUserId: focus,
      flag: state  //关注状态，0关注目标用户，1取消对目标用户的关注
    };
    service.requestAddFans(parma, function (data) {
      that.focusService()
    });
  },
  /**
   * view触底监听
   */
  onReachBottom: function () {
    if (this.data.hasNextPage == false) {
      return
    }
    this.setData({
      startPage: this.data.startPage + 1
    })

    this.requestSource(true)
  },
  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  //分享
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg
    var path = "mineSubs/pages/homepage/personalhomepage?userId=" + app.globalData.userId + "&type=" + 2
    return {
      title: title,
      path: path,

      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },
  //返回首页
  homeBtnClick: function () {
    wx.switchTab({
      url: '/pages/home/home'
    })
    that.setData({ homeHidden: true })
  },
  showTost: function (str) {
    wx.showToast({
      title: str,
      icon: 'succes',
      duration: 1000,
      mask: true
    })
  },

  /**
    * 生命周期函数--监听页面隐藏
    */
  onHide: function () {
    console.log("生命周期函数--监听页面隐藏");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("生命周期函数--监听页面卸载");
  },
  onPullDownRefresh: function () {
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 500);
  },
})